package cn.kshost.zqyg.manager.helper;

import cn.kshost.zqyg.model.entity.system.SysMenu;
import cn.kshost.zqyg.model.vo.system.SysMenuVo;
import java.util.LinkedList;
import java.util.List;

public class MenuHelper {

    /**
     * 构建树
     * @param sysMenuList
     * @return
     */
    public static List<SysMenuVo>  build(List<SysMenu> sysMenuList){

        if(sysMenuList==null||sysMenuList.size()==0){
            return null;
        }
        List<SysMenuVo> sysMenuVoList = new LinkedList<>();
        for(SysMenu sysMenu : sysMenuList){
          if (sysMenu.getParentId() == 0 ){
              SysMenuVo sysMenuVo = buildNode(sysMenu, sysMenuList);
              sysMenuVoList.add(sysMenuVo);
          }
        }
        return sysMenuVoList;
    }

    /**
     * 构建用户菜单菜单单个节点
     * @param sysMenu
     * @param sysMenuList
     * @return
     */
    private static SysMenuVo buildNode(SysMenu sysMenu, List<SysMenu> sysMenuList){
        SysMenuVo sysMenuVo = new SysMenuVo();
        sysMenuVo.setName(sysMenu.getComponent());
        sysMenuVo.setTitle(sysMenu.getTitle());
        sysMenuVo.setChildren(new LinkedList<>());
        for(SysMenu sysMenuChild : sysMenuList){
            if (sysMenuChild.getParentId() == sysMenu.getId() ){

                sysMenuVo.getChildren().add(buildNode(sysMenuChild, sysMenuList));
            }
        }
        return sysMenuVo;
    }
}